package top.xzxsrq.fileviewutils.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import top.xzxsrq.fileviewutils.enums.RoleEnum;
import top.xzxsrq.fileviewutils.service.UserDetailsServiceImpl;
import top.xzxsrq.web.utils.WebUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@TableName("`sys_user`")
public class SysUser implements UserDetails {
    @TableId("user_id")
    private String userId;
    private String username;
    private String password;
    private String userpath = "/";
    private String roleName;
    private String creatorId;
    private String userSysConfig;
    @TableField(exist = false)
    @JsonIgnore
    private Collection<GrantedAuthority> authorities;
    private String auths;

    public void setRoleName(String roleName) {
        this.roleName = roleName;
        if(this.authorities == null) {
            this.authorities = new ArrayList<>();
        }
        if(this.roleName == null) {
            return;
        }
        String[] split = this.roleName.split(WebUtils.split);
        for (String s : split) {
            authorities.add(new SimpleGrantedAuthority("ROLE_" + s));
        }
    }

    public void setAuths(String auths) {
        this.auths = auths;
        if(this.authorities == null) {
            this.authorities = new ArrayList<>();
        }
        if(this.auths == null) {
            return;
        }
        String[] split = this.auths.split(WebUtils.split);
        for (String s : split) {
            authorities.add(new SimpleGrantedAuthority(s));
        }
    }

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return getGrantedAuthority();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public boolean isAdmin() {
        boolean equals = RoleEnum.admin.name().equals(this.getRoleName());
        if (equals) {
            return true;
        }
        return false;
    }

    public boolean isInitAdmin() {
        boolean equals = UserDetailsServiceImpl.INIT_ADMIN_ROLE_NAME.equals(this.getRoleName());
        if (equals) {
            return true;
        }
        return false;
    }

    @JsonIgnore
    public List<GrantedAuthority> getGrantedAuthority() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("ROLE_" + this.getRoleName()));
        if(this.auths != null) {
            String[] split = this.auths.split(WebUtils.split);
            for (String s : split) {
                authorities.add(new SimpleGrantedAuthority(s));
            }
        }
        this.authorities = authorities;
        return authorities;
    }
}
